//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-5-16
// @Time:上午9:55
//

#include "TcpClient.h"

TcpClient::TcpClient(std::string serverip, int port) : m_buf(BUFF_LEN, 0),
                                                       m_endpoint(address_type::from_string(serverip), port)
{
    start();
}

void TcpClient::start()
{
    std::cout << "client start()" << std::endl;
    socket_ptr socket(new socket_type(m_io));
    //连接服务器
    socket->async_connect(m_endpoint,
                          bind(&TcpClient::conn_handler, this, boost::asio::placeholders::error, socket));

    this_socket = socket;

}

void TcpClient::conn_handler(const boost::system::error_code &ec, TcpClient::socket_ptr socket)
{
    if (ec)
    {
        return;
    }
    std::cout << "recive from " << socket->remote_endpoint().address().to_string() << std::endl;
    socket->async_read_some(boost::asio::buffer(m_buf),
                            bind(&TcpClient::read_handler, this, socket, boost::asio::placeholders::error,
                                 boost::asio::placeholders::bytes_transferred));

}

void TcpClient::read_handler(TcpClient::socket_ptr socket, const boost::system::error_code &ec,
                             std::size_t bytes_transferred)
{
    std::cout << socket->remote_endpoint().address().to_string() << std::endl;
    if (ec)
    {
        std::cout << ec.message() << std::endl;
        return;
    }
    std::string readMessage = std::string(&m_buf.front(), bytes_transferred);
    if (reacCallback != nullptr)
    {
        reacCallback(this, readMessage);
    }
    std::cout << "recieve msg: " << readMessage << std::endl;
    socket->async_read_some(boost::asio::buffer(m_buf),
                            bind(&TcpClient::read_handler, this, socket, boost::asio::placeholders::error,
                                 boost::asio::placeholders::bytes_transferred));
}

void TcpClient::writemsg(std::string &msg)
{
//    if (msg == "exit")
//    {//exit 是退出命令
//        this_socket->close();
//        work->~work();
//    } else
//    {
    this_socket->async_write_some(boost::asio::buffer(msg + suffix),
                                  boost::bind(&TcpClient::write_handler, this, this_socket,
                                              boost::asio::placeholders::error));
//    }
}

void TcpClient::write_handler(TcpClient::socket_ptr socket, const boost::system::error_code &ec)
{
    if (ec)
    {
        std::cout << "send fail" << ec.message() << std::endl;
        return;
    }


    std::cout << "send msg ok" << std::endl;
    socket->async_read_some(boost::asio::buffer(m_buf),
                            bind(&TcpClient::read_handler, this, socket, boost::asio::placeholders::error,
                                 boost::asio::placeholders::bytes_transferred));
}

void TcpClient::setReacCallback(TcpCallbackFunction reacCallback)
{
    TcpClient::reacCallback = reacCallback;
}

void TcpClient::run()
{
    m_io.run();
}

void TcpClient::stop()
{
    this_socket->close();
//    work->~work();
    m_io.stop();
}
